package com.carepad.action.selectclient;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.carepad.action.selectclient.AbstractSelectClientAction;
import com.carepad.entity.mapper.SelectClient;
import com.carepad.service.impl.SelectClientServiceImpl;

public class SelectClientViewAction extends AbstractSelectClientAction{

	private static final long serialVersionUID = 1L;

	private List<SelectClient> selectClientList;
	private SelectClient searchClient;
	
	private String name;
	private String add;
	private String hcs;
	private String isBtnClick;
	
	@Autowired
	private SelectClientServiceImpl selectClientServiceImpl;
	
	public String execute() {
		selectClientList = selectClientService.findClientBasicUserSummaryList();
		return SUCCESS;
	}
	
	public String searchClient(){
		try{
			
			if(searchClient != null){
				
				if(searchClient.getUserName() != null)
					name = getCheckedString(searchClient.getUserName());
				
				if(isBtnClick != null){
					if(isBtnClick.equals("true") && searchClient.getUserName().equals(""))
						name = "%%";
				}
				
				if(searchClient.getResidance() != null)				
					add = getCheckedString(searchClient.getResidance());
				
				if(searchClient.getHcsID() != null)	
					hcs = getCheckedString(searchClient.getHcsID());
				
			} else {
				
				SelectClient currentPatient = getSessionPatient();
				
				if(currentPatient != null){
					name = getCheckedString(currentPatient.getUserName());
				} else{
					
					name = "%%";				
				}				
			}
			
			selectClientList = selectClientServiceImpl.searchClientListByNameAddressID(name, add, hcs);
			
			if(selectClientList.size() == 0){
				addActionError(getText("selectclient.nodatafound"));				
			}
			return SUCCESS;
		}
		catch (Exception e) {
			e.printStackTrace();
			addActionError(getText("selectclient.error"));
			return SUCCESS;
		}
	}
	
	public List<SelectClient> getSelectClientList(){
		return selectClientList;
	}
	
	public void setSelectClientList(List<SelectClient> selectClientList){
		this.selectClientList = selectClientList;
	}

	public SelectClient getSearchClient() {
		return searchClient;
	}

	public void setSearchClient(SelectClient searchClient) {
		this.searchClient = searchClient;
	}
	
	private String getCheckedString(String str){
		
		String state = "%%";
		
		if(str.equals("")){
			state = "%*%";
		}
		else if(str.equals("*")){				
			state = "%%";
		}
		else {			
			state = "%" + str + "%";
		}
		
		return state;
	}

	public String getIsBtnClick() {
		return isBtnClick;
	}

	public void setIsBtnClick(String isBtnClick) {
		this.isBtnClick = isBtnClick;
	}
}
